<?php

/**
 * 后台管理相关接口
 *
 * @author Samuel
 * @since 2012.4.6
 */
class AdminController extends BaseController {

    public function run() {
        //$dbConfig = Indexer::getDbConfig( 1 );
        //$dbStatus = Indexer::getDbStatus();
        include TPL_DIR . 'index.php';
    }
    public function addDbConfigUser() {
        $host = $this->getParamString('host', false);
        $port = $this->getParamInt('port', false);
        $user = $this->getParamString('user', false);
        $passwd = $this->getParamString('passwd', false);
        $name = $this->getParamString('name', false);
        $status = $this->getParamInt('status', false);

        //添加配置
        $dbId = Indexer::addDbConfigUser($host, $port, $user, $passwd, $name);
        Indexer::addDbStatus($dbId, $status);

        //检查配置
        $indexerModel = Indexer::getDbInstance($dbId, Indexer::DB_TYPE_USER);
        $sql = 'SHOW TABLES FROM ' . $name;
        $res = $indexerModel->query($sql);
        $result = $indexerModel->res2Assoc($res);
        foreach ($result as $v) {
            foreach ($v as $value) {
                echo '<p>' . $value . '</p>';
            }
        }
    }

    public function updateDbConfigUser() {
        $dbId = $this->getParamInt('dbId', false);
        $host = $this->getParamString('host', false);
        $port = $this->getParamInt('port', false);
        $user = $this->getParamString('user', false);
        $passwd = $this->getParamString('passwd', false);
        $name = $this->getParamString('name', false);
        $status = $this->getParamInt('status', false);

        //保存配置
        $dbId = Indexer::updateDbConfigUser($dbId, $host, $port, $user, $passwd, $name);
        Indexer::setDbStatus($dbId, $status);

        //检查配置
        $indexerModel = Indexer::getDbInstance($dbId, Indexer::DB_TYPE_USER);
        $sql = 'SHOW TABLES FROM ' . $name;
        $res = $indexerModel->query($sql);
        $result = $indexerModel->res2Assoc($res);
        foreach ($result as $v) {
            foreach ($v as $value) {
                echo '<p>' . $value . '</p>';
            }
        }
    }

    public function removeDbConfigUser() {
        $dbId = $this->getParamInt('dbId', false);

        //保存配置
        Indexer::removeDbConfigUser($dbId);
        Indexer::removeDbStatus($dbId);

        echo '配置' . $dbId . '已经删除';
    }
    
    

}

?>
